package com.creative.xcx.controller;

import com.alibaba.excel.EasyExcel;
import com.creative.core.controller.BaseController;
import com.creative.core.domain.AjaxResult;
import com.creative.core.page.TableDataInfo;
import com.creative.xcx.domain.Banner;
import com.creative.xcx.service.BannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


/**
 * @author ling
 * @description 首页轮播图
 * @date 2025-02-24
 */
@RestController
@RequestMapping(value = "/Banner")
public class BannerController extends BaseController {

    @Resource
    private BannerService bannerService;

    /**
     * 新增
     *
     * @author ling
     * @date 2025/02/24
     **/
    @PostMapping
    public AjaxResult insert(@RequestBody Banner banner) {
        return toAjax(bannerService.insert(banner));
    }

    /**
     * 刪除单个
     *
     * @author ling
     * @date 2025/02/24
     **/
    @DeleteMapping("/{pId}")
    public AjaxResult delete(@PathVariable("pId") String pId) {
        return toAjax(bannerService.delete(pId));
    }

    /**
     * 刪除多个
     *
     * @author ling
     * @date 2025/02/24
     **/
    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody String[] ids) {
        return toAjax(bannerService.deleteBatch(ids));
    }

    /**
     * 更新
     *
     * @author ling
     * @date 2025/02/24
     **/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Banner banner) {
        return toAjax(bannerService.update(banner));
    }


    /**
     * 查询 根据主键 id 查询
     *
     * @author ling
     * @date 2025/02/24
     **/
    @GetMapping(value = "/{pId}")
    public AjaxResult selectById(@PathVariable("pId") String pId) {

        if (pId == null) {
            return error("参数为空");
        }
        return success(bannerService.selectById(pId.trim()));
    }

    /**
     * 查询 分页查询(带条件)
     *
     * @author ling
     * @date 2025/02/24
     **/
    @GetMapping("/list")
    public TableDataInfo selectBannerList(Banner banner) {
        return bannerService.selectBannerList(banner);
    }

    /**
     * 获取小程序轮播图数据
     *
     * @param banner 对象
     * @return {@link TableDataInfo }
     * @author ling
     * @date 2025/02/26
     */
    @GetMapping("/mp/list")
    public AjaxResult selectMpBannerList(Banner banner) {
        return success(bannerService.selectMpBannerList(banner));
    }

    /**
     * 导出
     *
     * @param ids 数组
     * @return {@link AjaxResult }
     * @author ling
     * @date 2025/02/26
     */
    @PostMapping("/export")
    public void export(@RequestBody String[] ids, HttpServletResponse response) throws IOException {
//        判断ids是否为空
        if (ids == null || ids.length == 0) {
            // ids 为空或未初始化时执行的代码
            throw new RuntimeException("ids为空");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("轮播图" + System.currentTimeMillis(), StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Banner.class)
                .sheet("轮播图")
                .doWrite(() -> {
                    // 查询数据
                    return bannerService.exportBatch(ids);
                });
        logger.info("导出成功");
    }
}
